package com.ouyang.servlet;

import com.ouyang.service.OdersService;
import com.ouyang.service.OdersServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/oders.do")
public class odersServlet extends HttpServlet {

    OdersService odersService = new OdersServiceImpl();

    @Override
    protected void doGet ( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost ( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (method.equals("list")) {
            this.getoderslist(req, resp);
        } else if (method.equals("add")) {
            this.addoders(req, resp);
        } else if (method.equals("del")) {
            this.del(req, resp);
        } else if (method.equals("update")) {
            this.update(req, resp);
        } else if (method.equals("search")) {
            this.search(req, resp);
        }


    }

    public void getoderslist ( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
        req.setAttribute("list", odersService.getlist());
        req.getRequestDispatcher("/admin/cdblist.jsp").forward(req, resp);

    }

    public void addoders ( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
        String OrderNumber = req.getParameter("OrderNumber");
        String cdbName = req.getParameter("cdbName");
        String RentPeople = req.getParameter("RentPeople");
        odersService.addoders(OrderNumber, cdbName, RentPeople);
        resp.sendRedirect("oders.do?method=list");
    }

    public void del ( HttpServletRequest req, HttpServletResponse resp ) throws IOException {
        Integer id = Integer.valueOf(req.getParameter("id"));
        odersService.deloders(id);
        resp.sendRedirect("oders.do?method=list");
    }

    public void update ( HttpServletRequest req, HttpServletResponse resp ) throws IOException {
        Integer id = Integer.valueOf(req.getParameter("id"));
        String ordersnumber = req.getParameter("odersnumber");
        String cdbname = req.getParameter("cdbname");
        String rentpeople = req.getParameter("rentpeople");
        odersService.update(id, ordersnumber, cdbname, rentpeople);
        resp.sendRedirect("oders.do?method=list");
    }


    public void search ( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
        String key = req.getParameter("key");
        String value = req.getParameter("value");
        req.setAttribute("list",odersService.search(key,value));
        req.getRequestDispatcher("/admin/cdblist.jsp").forward(req,resp);
    }
}
